package com.zhx.common.common.util.discover;

import cn.hutool.core.util.StrUtil;
import com.zhx.common.common.util.discover.doamin.UrlInfo;
import org.jsoup.nodes.Document;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 具有优先级的title查询器
 * PrioritizedUrlDiscover是我们的策略类，同时也是组装责任链的工厂。如果你调用它，它会按顺序执行责任链，直到解析出url标题。
 */
public class PrioritizedUrlDiscover extends AbstractUrlDiscover {

    private final List<UrlDiscover> urlDiscovers = new ArrayList<>(2);

    public PrioritizedUrlDiscover() {
        urlDiscovers.add(new WxUrlDiscover());
        urlDiscovers.add(new CommonUrlDiscover());
    }

    @Nullable
    @Override
    public String getTitle(Document document) {
        for(UrlDiscover urlDiscover : urlDiscovers){
            String urlTitle = urlDiscover.getTitle(document);
            if(StrUtil.isNotBlank(urlTitle)){
                return urlTitle;
            }
        }
        return null;
    }

    @Nullable
    @Override
    public String getDescription(Document document) {
        for (UrlDiscover urlDiscover : urlDiscovers) {
            String urlDescription = urlDiscover.getDescription(document);
            if (StrUtil.isNotBlank(urlDescription)) {
                return urlDescription;
            }
        }
        return null;
    }

    @Nullable
    @Override
    public String getImage(String url, Document document) {
        for (UrlDiscover urlDiscover : urlDiscovers) {
            String urlImage = urlDiscover.getImage(url, document);
            if (StrUtil.isNotBlank(urlImage)) {
                return urlImage;
            }
        }
        return null;
    }
}
